package com.zhongzhi.nn.push.server;


import com.zhongzhi.nn.push.configure.ApplicationConstants;
import com.zhongzhi.nn.push.dao.ProjectInfoMapper;
import com.zhongzhi.nn.push.dataSource.MybatisConstants;
import com.zhongzhi.nn.push.model.LandInfo;
import com.zhongzhi.nn.push.model.ProjectInfo;
import com.zhongzhi.nn.push.util.PushNNUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * 项目入场推送
 */
public class PushProjectInfo {


    //测试地址
    //private  String url="https://www.nnggzy.org.cn:3080/gxnnztbtest1/JSGCZtbMis2_JZ/Pages/Webservice/TdjyInfo.asmx";
    // private String url="https://www.nnggzy.org.cn:3080/gxnnztbtest/JSGCZtbMis2_JZ/Pages/Webservice/TdjyInfo.asmx";
    //正式地址
    private String url = ApplicationConstants.url;
    public static Log log = LogFactory.getLog(PushNoticeInfo.class);

    public  String push() {
        String xml = null;
        String webServiceXml = null;
        String message = "0";//没有土地数据
        //土地
        PushLandInfo land = new PushLandInfo();
        List<ProjectInfo> list = this.get();
        List<LandInfo> lands = null;
        for (ProjectInfo project : list) {
            lands = land.get(project.getProjectGuid());
            //公告有资源后再推
            if (lands.size() > 0) {
                xml = PushNNUtils.getProjectResultXml(project, lands);
                log.info("项目备案数据："+xml);
                webServiceXml = PushNNUtils.getWebServiceXml(xml, "SetTDInfo");
                String resultxml = PushNNUtils.sendForWebService(url, webServiceXml);
                String result = PushNNUtils.getXmlAttribute(resultxml);
                if("1".equals(result)){
                    updateData(project.getProjectGuid());
                }
                if ("1".equals(result)) {
                    message = "SUCCESS";
                    log.info("项目备案推送成功！");
                } else {
                    message = result;
                    log.error("项目备案推送失败："+result);
                }
            }
        }
        return  message;
    }


    /**
     * 查询推送数据
     *
     * @return
     */
    public List<ProjectInfo> get() {
        List<ProjectInfo> list = null;
        SqlSession sqlSession = null;
        try {
            sqlSession = MybatisConstants.getSqlSession();
            ProjectInfoMapper dao = sqlSession.getMapper(ProjectInfoMapper.class);
            list = dao.getData();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(sqlSession != null) {
                sqlSession.close();
            }
        }
        return list;
    }

    public Integer updateData(String projectId) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MybatisConstants.getSqlSession();
            ProjectInfoMapper dao = sqlSession.getMapper(ProjectInfoMapper.class);
            Integer count = dao.updateData(projectId);
            sqlSession.commit();
            if(count>0){
                System.out.println("状态修改成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(sqlSession != null) {
                sqlSession.close();
            }
        }
        return 0;
    }
}
